package com.ebt.m.wiki.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import com.ebt.m.R;
import com.ebt.m.utils.MathUtil;

/**
 * Created by andy on 16-7-25.
 */
public class ProductSingAgeItem extends View {
    private int mWidth;
    private int mHeight;
    private float maxValue;
    private double value;
    private float rectWidth;
    private int color;
    private String title;
    private int maxContentWidth;
    private float titleWidth = 270;
    private int marginLeft = 10;
    private int minRectWidth = 13;
    private float drawY;
    private int textSize = 30;

    private Paint mTextPaint;
    private Paint mPricePaint;
    private Paint mRectPaint;

    public ProductSingAgeItem(Context context) {
        this(context, null);
    }

    public ProductSingAgeItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProductSingAgeItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        textSize = (int) context.getResources().getDimension(R.dimen.product_age_item);
        initPaint();
    }

    private void initPaint() {
        mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setTextSize(textSize);
        mTextPaint.setTextAlign(Paint.Align.RIGHT);
        mPricePaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        mPricePaint.setTextSize(textSize);
        mPricePaint.setTextAlign(Paint.Align.LEFT);
        mPricePaint.setColor(Color.parseColor("#B76073"));
        mRectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
        updateDimen();
    }

    private void updateDimen() {
        float priceWidth = mPricePaint.measureText(MathUtil.getFormatedDoubleString(value) + ":");
        maxContentWidth = (int) (mWidth - titleWidth - priceWidth);
        if (maxValue == 0) {
            rectWidth = 0;
        } else {
            rectWidth = (float) (value * maxContentWidth / maxValue);
        }
        if (value > 0 && rectWidth < minRectWidth) {
            rectWidth = minRectWidth;
        }
        RectF rectF = new RectF(0, 0, titleWidth, mHeight);
        drawY = rectF.centerY() + Math.abs(mTextPaint.ascent()) - (mTextPaint.descent() - mTextPaint.ascent()) / 2;

    }

    public void update(String maxTitle, float maxValue, double value, int color, String title) {
        this.maxValue = maxValue;
        this.value = value;
        this.color = color;
        this.title = title;
        titleWidth = mTextPaint.measureText(maxTitle + ":");
        mRectPaint.setColor(color);
        if (mWidth != 0) {
            updateDimen();
            invalidate();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mWidth == 0) return;
        canvas.drawText(title + ":", titleWidth, drawY, mTextPaint);
        canvas.drawRect(titleWidth + marginLeft, 0, titleWidth + rectWidth - marginLeft, mHeight, mRectPaint);
        canvas.drawText(String.valueOf(MathUtil.getFormatedDoubleString(value)), titleWidth + rectWidth, drawY, mPricePaint);
    }
}
